﻿using System;
using System.Linq;
using System.Xml.Linq;
using _8BitMusic.core;
using MySpaceID.SDK;
using MySpaceID.SDK.Models.V1;
using MySpaceID.SDK.Api;
using MySpaceID.SDK.Context;

namespace _8BitMusic
{
    public partial class Default : System.Web.UI.Page
    {
        public int UserId { get; set; }
        //public MySpace MySpace { get; set; }
        public RestV1 MySpace { get; set; }
        public ExtendedProfile ExtendedProfile { get; set; }
        public UserFriends Friends {get; set;}
        public XDocument Activities { get; set; }
        public XDocument FriendActivities { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            //FakeLogin();
            if (IsLoggedIn())
                LoggedIn();
            else
                LoggedOut();
        }

        private void LoggedIn()
        {
            this.UserId = Convert.ToInt32(Session["userID"]);
	        //this.MySpace = new MySpace(Constants.ConsumerKey,Constants.ConsumerSecret,MySpaceID.SDK.Config.ApplicationType.MySpaceID,Session["access_token_key"].ToString(),Session["access_token_secret"].ToString());
            var context = new OffsiteContext(Constants.ConsumerKey, Constants.ConsumerSecret, Session["access_token_key"].ToString(), Session["access_token_secret"].ToString());
            this.MySpace = new RestV1(context);
	
	        this.ExtendedProfile = this.MySpace.GetExtendedProfile(this.UserId);
	        this.Friends = this.MySpace.GetFriends(this.UserId);
	
	        this.Activities = this.MySpace.GetActivities(this.UserId);
            this.FriendActivities = this.MySpace.GetFriendActivities(this.UserId);
        }

        private void LoggedOut()
        { }

        public bool IsLoggedIn()
        {
            
            return(Session["userID"] != null && Session["access_token_key"] != null && Session["access_token_secret"] != null);
            
        }

        public string GetActivitiesHtml()
        {
            if (this.Activities == null)
                return "ACTIVITIES ERROR";
            XNamespace xns = "http://www.w3.org/2005/Atom";
            var entries = this.FriendActivities.Descendants(xns + "entry");
            var songEntries = entries.Where(x => (x.Element(xns + "category").Attribute("label") != null && (x.Element(xns + "category").Attribute("label").Value == "SongUpload" ||
               x.Element(xns + "category").Attribute("label").Value == "ProfileSongAdd")));
            string activityOutput = "<ul class='activitiesContainer'>";
            foreach (var entry in songEntries)
            {
                activityOutput += string.Format("<li class='activityItem {0}'>{1}</li>", entry.Element(xns + "category").Attribute("label").Value, entry.Element(xns + "content").FirstNode.ToString());

            }
            activityOutput += "</ul>";
            return activityOutput;
        }

        public string GetFriendActivitiesHtml()
        {
            if (this.FriendActivities == null)
                return "ACTIVITIES ERROR";
            XNamespace xns = "http://www.w3.org/2005/Atom";
            var entries = this.FriendActivities.Descendants(xns + "entry");
            var songEntries = entries.Where(x => (x.Element(xns + "category").Attribute("label") != null && (x.Element(xns + "category").Attribute("label").Value == "SongUpload" ||
               x.Element(xns + "category").Attribute("label").Value == "ProfileSongAdd")));
            string activityOutput = "<ul class='activitiesContainer'>";
            foreach (var entry in songEntries)
            {
                activityOutput += string.Format("<li class='activityItem {0}'>{1}</li>", entry.Element(xns + "category").Attribute("label").Value, entry.Element(xns + "content").FirstNode.ToString());

            }
            activityOutput += "</ul>";
            return activityOutput;
        }

    }
}
